package com.example.jwttest;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class JwtTest {
    public static void main(String[] args) {
        //加码
        Key key= Keys.hmacShaKeyFor("pfttre+euQ+rmoSN/mckqGTJRe66gepHbOY07UuPpBg=".getBytes(StandardCharsets.UTF_8));
        Map<String,Object> claims=new HashMap<>();
        claims.put("id",1);
        claims.put("name","杨森");
        String compact= Jwts.builder()
                .setClaims(claims)
                .signWith(key)
                .compact();
        System.out.println(compact);

//解码
            Claims parsedClaims = Jwts.parserBuilder()
                    .setSigningKey(key)
                    .build()
                    .parseClaimsJws(compact)
                    .getBody();
            // 解析出负载中的内容
        System.out.println(parsedClaims);
    }
}
